<?php

namespace app\speedDating\model;

use app\speedDating\interfaces\ServiceStatus;
use app\speedDating\model\traits\SimpleModel;
use think\Model;

class SddFinanceAccount extends Model
{

    use SimpleModel;

    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'sdd_finance_account';

    protected static $createTimeField = 'gmt_create';

    protected static $updateTimeField = 'gmt_modified';

    /**
     * @see SimpleModel
     * @var string[] 字段数组
     */
    protected static $SimpleMainField = [
        'id',
        'user_id',
        'account_type',
        'total_amount',
        'balance',
        'is_banned'
    ];

    /**
     * 创建资金账户
     * @param $userID
     * @param $account_type
     * @return SddFinanceAccount|array|\think\Collection
     */
    public static function createAccount($userID, $account_type)
    {
        $insData['user_id'] = $userID;
        $insData['account_type'] = $account_type;

        return self::toInsert($insData);
    }

    /**
     * 使用会员ID与账户类型，查询账户ID
     * @param $user_id
     * @param int $account_type
     * @return SddFinanceAccount|array|\think\Collection
     */
    public static function getAccountID($user_id, int $account_type = ServiceStatus::ACCOUNT_TYPE_BALANCE)
    {
        return self::where('user_id', $user_id)->where('account_type', $account_type)->value('id');
    }

    /**
     * 使用会员ID与账本类型，查询账户数据
     * @param $user_id
     * @param int $account_type
     * @return SddFinanceAccount|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAccountByUserID($user_id, int $account_type = ServiceStatus::ACCOUNT_TYPE_BALANCE)
    {
        return self::field(self::$SimpleMainField)->where('user_id', $user_id)->where('account_type', $account_type)->find();
    }

    /**
     * 使用会员ID,查所有类型账户
     * @param $user_id
     * @return SddFinanceAccount[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAccountListByUserID($user_id)
    {
        return self::where('user_id', $user_id)->field(self::$SimpleMainField)->select();
    }

}

